package com.omesoft.hksnore.util.data;

import android.content.Context;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class MyDateUtil {
    public static final int EVERYTIME = 0;
    public static final int EVERYDAY = 1;
    public static final int EVERYWEEK = 2;
    public static final int EVERYMONTH = 3;

    /**
     * 下拉刷新时间的格式
     */
    public static SimpleDateFormat getSimpleDateFormat() {
        return new SimpleDateFormat("yyyy年MM月dd日   HH:mm:ss");
    }


    public static String getStartOrEndTime( ) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String dateStr = dateFormat.format(date);
        return dateStr;
    }


    /**
     * 如果不传格式。。就用默认的格式:yyyy-MM-dd HH-mm-ss.SSS
     */
    public static String getDateFormatToString(String format) {
        Date date = new Date();
        if (format == null) {
            format = "yyyy-MM-dd HH:mm:ss.SSS";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(format);
        String dateStr = dateFormat.format(date);
        return dateStr;
    }

    /**
     *
     * 获取update的时间
     * */
    public static String getUpDateTime() {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = dateFormat.format(date);
        return dateStr;
    }
    /**
     * :yyyy-MM-dd
     */
    public static String getStringDD(String date) {
        String[] dateStr = date.split(" ");
        // System.out.println("dateStr[0]:" + dateStr[0]);
        return dateStr[0];
    }

    /**
     * 显示日期格式为：yyyy-MM-dd
     */
    public static String getDateFormatToYMD(String date) {
        String targetString = "";
        if (date != null) {
            date = getStringDD(date);
            String[] dateStr = date.split("/");

            targetString = dateStr[0] + "-" + dateStr[1] + "-" + dateStr[2];
            // System.out.println("dateStr.length="+String.valueOf(dateStr.length));
            // System.out.println(dateStr[0]+"-"+dateStr[1]+"-"+dateStr[2]);

        }
        return targetString;
    }

    public static int getDateDays(String date1, String date2) {
        System.out.println("date2=" + date2);
        System.out.println("date1=" + date1);
        date1 = date1.split(" ")[0];
        date2 = date2.split(" ")[0];
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int days = 0;
        try {
            Date date = sdf.parse(date1);// 通过日期格式的parse()方法将字符串转换成日期
            Date dateBegin = sdf.parse(date2);
            // System.out.println("getDateDays:date" + date);
            // ;
            // System.out.println("dateBegin" + dateBegin);
            ;
            long betweenTime = date.getTime() - dateBegin.getTime();
            days = (int) (betweenTime / 1000 / 60 / 60 / 24);
        } catch (Exception e) {
            // System.out.println(e.toString());
        }
        // System.out.println("day==" + days);
        return days;
    }

    public static String getDate(Context context, String date) {
        // System.out.println("date=" + date);
        int days = getDateDays(getDateFormatToString(null), date);
        // System.out.println("days===========" + days);
        String[] dateStr = date.split(" ");
        String[] i = dateStr[1].split(":");
        int h, m;
        h = Integer.valueOf(i[0]);
        m = Integer.valueOf(i[1]);
        dateStr[1] = i[0] + ":" + i[1];
        if (days == 0) {
            if (h > 12) {
                h = h - 12;
                date = "今天，下午 " + h + ":" + m;
            } else {
                date = "今天，上午 " + h + ":" + m;
            }
        } else if (days == 1) {
            if (h > 12) {
                h = h - 12;
                date = "昨天，下午 " + h + ":" + m;
            } else {
                date = "昨天，上午 " + h + ":" + m;
            }
        } else {
            date = dateStr[0];
        }
        return date;
    }

    public static long getInBedTime(){
        Date dt = new Date();
        long time = dt.getTime();
        return time;

    }


    public static String getSleepId(){//sleepId 年月日时分秒毫秒加上四位随机数字
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        int temp = (int) (Math.random()*10000);
        while (temp/1000<1){
            temp = (int) (Math.random()*10000);
        }
        return df.format(date)+temp;
    }
    public static String getSnoreStartTime(){
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS");

        return df.format(date);
    }

    public static String getChatTime(String str1) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df1 = new SimpleDateFormat("MM-dd HH:mm");
        Date one = null;
        try {
            one = df.parse(str1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (one != null) {
            return df1.format(one);
        } else {
            return "";
        }
    }




    /**
     * @param @param  str1
     * @param @param  str2
     * @param @return 设定文件
     * @return String    返回类型
     * @throws
     * @Title: getDistanceTimeExceed
     * @Description: TODO(判断两个时间相差是否超过一分钟)
     */
    public static boolean getDistanceTimeExceed(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date one;
        Date two;
        try {
            one = df.parse(str1);
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            int min = (int) (diff / (60 * 1000));
            if (min >= 1) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /*
     * *
     * 两个时间相差距离多少天多少小时多少分多少秒
     *
     * @param str1 时间参数 1 格式：1990-01-01 12:00:00
     *
     * @param str2 时间参数 2 格式：2009-01-01 12:00:00
     *
     * @return String 返回值为：xx天xx小时xx分xx秒
     */
    public static String getDistanceTime(String str2) {
        DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Date one;
        Date two;
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        try {
            one = new Date();
            two = df.parse(str2);
            long time1 = one.getTime();
            long time2 = two.getTime();
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            day = diff / (24 * 60 * 60 * 1000);
            hour = (diff / (60 * 60 * 1000) - day * 24);
            min = ((diff / (60 * 1000)) - day * 24 * 60 - hour * 60);
            sec = (diff / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day > 0 ? day + "天前" : "今天";
    }


    /**
     * 把String类型时间转化城long
     * */
    public static long stringTOlongDate(String time){
        long lTime=0;
        try{

            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt2 = sdf.parse(time);
            //继续转换得到秒数的long型
           lTime = dt2.getTime() / 1000;

        }catch (Exception e){
            e.printStackTrace();

        }
        return lTime;
    }
    /**
     * 把String类型时间转化城long 毫秒
     * */
    public static long stringTOlongDate2(String time){
        long lTime=0;
        try{

            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            Date dt2 = sdf.parse(time);
            //继续转换得到秒数的long型
            lTime = dt2.getTime() / 1000;

        }catch (Exception e){
            e.printStackTrace();

        }
        return lTime;
    }
    /**
     * 把String类型时间转化城long 毫秒
     * */
    public static long stringTOlongDate3(String time){
        long lTime=0;
        try{

            SimpleDateFormat sdf= new SimpleDateFormat("yyyyMMddHHmmssSSS");
            Date dt2 = sdf.parse(time);
            //继续转换得到秒数的long型
            lTime = dt2.getTime() / 1000;

        }catch (Exception e){
            e.printStackTrace();

        }
        return lTime;
    }
    /**
     * 把String类型时间转化城long 毫秒
     * */
    public static String longTOString(long time){
            String sTime="";
        try{

            SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date dt2 = new Date(time);
            //继续转换得到秒数的long型

            sTime =sdf.format(dt2).toString();
        }catch (Exception e){
            e.printStackTrace();

        }
        return sTime;
    }
    // public static String getY_m_d(String y_m_d) {
    // SimpleDateFormat dateFormat = new
    // SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    // Date date = null;
    // try {
    // date = dateFormat.parse(y_m_d);
    // } catch (Exception e) {
    // Log.e("test","getY_m_d_e.getMessage():"+e.getMessage());
    // }
    // dateFormat = new SimpleDateFormat("yy-MM-dd");
    // y_m_d = dateFormat.format(date);
    // return y_m_d;
    // }

    public static String getH_Min(String h_min) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
        Date date = null;
        try {
            date = dateFormat.parse(h_min);
        } catch (Exception e) {
            // Log.e("test","getH_Min_e.getMessage():"+e.getMessage());
        }
        h_min = dateFormat.format(date);
        return h_min;
    }

    /**
     * 是否早于今天
     */
    public static boolean isBeforeToDay(String y_m_d) {
        int compareTo = 0;
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = dateFormat.parse(y_m_d);

            Date nowDate = dateFormat.parse(dateFormat.format(new Date()));
            compareTo = date.compareTo(nowDate);

        } catch (Exception e) {
            // Log.e("test","isBeforeToDay_e.getMessage():"+e.getMessage());
        }
        if (compareTo < 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 返回拼写好格式为2008-11-05 21:33
     */
    public static String getSpellTime(int curYears, int curMonths, int curDays, int curHours, int curMinutes) {
        StringBuilder y_m_d = getSpell_Y_M_D(curYears, curMonths, curDays);
        StringBuilder h_M = getSpell_H_M(curHours, curMinutes);
        return y_m_d.toString() + h_M.toString();
    }

    public static StringBuilder getSpell_H_M(int curHours, int curMinutes) {
        StringBuilder h_M = new StringBuilder();
        if (curHours > 9) {
            h_M.append(curHours);
        } else {
            h_M.append("0" + curHours);
        }
        h_M.append(":");
        if (curMinutes > 9) {
            h_M.append(curMinutes);
        } else {
            h_M.append("0" + curMinutes);
        }

        return h_M;
    }

    public static StringBuilder getSpell_Y_M_D(int curYears, int curMonths, int curDays) {
        StringBuilder y_m_d = new StringBuilder();
        y_m_d.append(curYears);
        y_m_d.append("-");
        if (curMonths + 1 > 9) {
            y_m_d.append(curMonths + 1);
        } else {
            y_m_d.append("0" + (curMonths + 1));
        }
        y_m_d.append("-");
        if (curDays > 9) {
            y_m_d.append(curDays);
        } else {
            y_m_d.append("0" + curDays);
        }
        y_m_d.append(" ");
        return y_m_d;
    }

    public static int[] formateDate(String date) {
        int[] dates = new int[3];
        String[] dateStr = date.split("-");
        for (int i = 0; i < dateStr.length; i++)
            dates[i] = Integer.valueOf(dateStr[i]);
        return dates;
    }

    public static StringBuilder getSpell_M_D(int curMonths, int curDays) {
        StringBuilder y_m_d = new StringBuilder();

        if (curMonths + 1 > 9) {
            y_m_d.append(curMonths + 1);
        } else {
            y_m_d.append("0" + (curMonths + 1));
        }
        y_m_d.append("-");
        if (curDays > 9) {
            y_m_d.append(curDays);
        } else {
            y_m_d.append("0" + curDays);
        }
        y_m_d.append(" ");
        return y_m_d;
    }

    // 给定一个日期，返回指定格式的日期
    public static String formateDate2(Date date) {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
        String formateDate = dateformat.format(date);
        return formateDate;
    }

    // 把String转为Date
    public static Date StringToDate(String brithday) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = simpleDateFormat.parse(brithday);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }


    }
    // 给定一个日期，返回指定格式的日期
    public static String DateToString(Date date) {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy年MM月dd日");
        String formateDate = dateformat.format(date);
        return formateDate;
    }
    // 给定一个日期，返回指定格式的日期
    public static String formateDate3(Date date) {
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String formateDate = dateformat.format(date);
        return formateDate;
    }

    /**
     * :yyyy-MM-dd:HH:mm:ss
     */
    public static String getStringSS(String date) {
        String[] dateStr = date.split("\\.");
        return dateStr[0];
    }

    public static String[] getYMDString(String dateStr) {
        String[] ymd = dateStr.split("-");
        return ymd;

    }

    public static Date getStringToDate(String dateString) {
        Calendar calendar = Calendar.getInstance();
        if (dateString != null) {
            String[] ymdString = dateString.split("/");
            calendar.set(Integer.parseInt(ymdString[0]), Integer.parseInt(ymdString[1]) - 1,
                    Integer.parseInt(ymdString[2]));
        }
        return calendar.getTime();

    }

    public static Date getStringToDate2(String dateString) {
        Calendar calendar = Calendar.getInstance();
        if (dateString != null) {
            String[] ymdString = dateString.split("-");
            calendar.set(Integer.parseInt(ymdString[0]), Integer.parseInt(ymdString[1]) - 1,
                    Integer.parseInt(ymdString[2]));
        }
        return calendar.getTime();

    }

    public static int getIntWeek(String dateString) {
        Calendar calendar = Calendar.getInstance();
        if (dateString != null) {
            String[] ymdString = dateString.split("-");
            calendar.set(Integer.parseInt(ymdString[0]), Integer.parseInt(ymdString[1]) - 1,
                    Integer.parseInt(ymdString[2]));
        }
        return calendar.get(Calendar.DAY_OF_WEEK);

    }

    // 获得当前日期与本周一相差的天数
    private static int getMondayPlus() {
        Calendar cd = Calendar.getInstance();
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    // 获得本周星期一的日期
    public static Calendar getCurrentMonday() {
        int mondayPlus = getMondayPlus();
        Calendar currentDate = Calendar.getInstance();
        currentDate.add(Calendar.DATE, mondayPlus);
        return currentDate;
    }

    // 获得下周星期一的日期
    public static Calendar getForWeekMonday(int week) {
        int mondayPlus = getMondayPlus();
        Calendar NextDate = Calendar.getInstance();
        NextDate.add(Calendar.DATE, mondayPlus + 7 * week);
        return NextDate;
    }

    // 获得相应周的周日的日期
    public static Calendar getSunday(int week) {
        int mondayPlus = getMondayPlus();
        Calendar currentDate = Calendar.getInstance();
        currentDate.add(Calendar.DATE, mondayPlus + 7 * week + 6);
        return currentDate;
    }

    public static int daysBetween(Date early, Date late) {
        Calendar calst = Calendar.getInstance();
        Calendar caled = Calendar.getInstance();
        calst.setTime(early);
        caled.setTime(late);
        // 设置时间为0时
        calst.set(Calendar.HOUR_OF_DAY, 0);
        calst.set(Calendar.MINUTE, 0);
        calst.set(Calendar.SECOND, 0);
        caled.set(Calendar.HOUR_OF_DAY, 0);
        caled.set(Calendar.MINUTE, 0);
        caled.set(Calendar.SECOND, 0);
        // 得到两个日期相差的天数
        int days = ((int) (caled.getTime().getTime() / 1000) - (int) (calst.getTime().getTime() / 1000)) / 3600 / 24;
        return days;

    }

    // 获得系统当前日期时间：yyyy-MM-dd
    public static String getSystemDate() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = sDateFormat.format(new Date());
        return date;
    }

    // 获得系统当前日期时间：yyyy-MM-dd HH:mm
    public static String getSystemDate2() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String date = sDateFormat.format(new Date());
        return date;
    }

    // 获得系统当前日期时间：yyyy-MM-dd HH:mm:ss
    public static String getSystemDate3() {
        SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String date = sDateFormat.format(new Date());
        return date;
    }

    // 获取过去时间点距离现在多久
    public static String getTimeInterval(String date) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat df2 = new SimpleDateFormat("yyyy-MM-dd");
        DateFormat df3 = new SimpleDateFormat("MM-dd");
        Date now;
        Date beforeTime = null;
        String string = "";
        long day = 0;
        long hour = 0;
        long min = 0;
        long sec = 0;
        int now_year = 0, before_year = 0;
        try {
            now = new Date();
            beforeTime = df.parse(date);
            now_year = now.getYear();
            before_year = beforeTime.getYear();
            long time1 = now.getTime();
            System.out.println("time1" + time1);
            long time2 = beforeTime.getTime();
            System.out.println("time2" + time2);
            long diff;
            if (time1 < time2) {
                diff = time2 - time1;
            } else {
                diff = time1 - time2;
            }
            System.out.println("diff" + diff);
            day = diff / (24 * 60 * 60 * 1000);
            hour = diff / (60 * 60 * 1000);
            min = diff / (60 * 1000);
            sec = diff / 1000;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (sec >= 60) {
            if (min >= 60) {
                if (hour >= 24) {
                    if (day > 2) {
                        if (now_year == before_year) {
                            string = df3.format(beforeTime);
                        } else {
                            string = df2.format(beforeTime);
                        }
                    } else {
                        string = day + "天前";
                    }

                } else {
                    string = hour + "小时前";
                }
            } else {
                string = min + "分钟前";
            }
        } else {
            string = "刚刚";
        }

        return string;
    }
}
